<?php

declare(strict_types=1);

namespace app\Index\controller;
use app\BaseController;
use app\common\model\server\Setting;
use think\facade\Cookie;
use think\facade\View;
/**
 * 设置管理
 * Class Group
 * @package app\admin\controller\system
 */
class Index extends BaseController
{
    protected $setting;

    public function index(){
		if(!cookie('install')){
			$settingModel = new Setting;
            $settings = $settingModel->find(1);
            $domain = $this->request->domain(); // 获取当前域名
            $appName = $settings->name;
            $filename =  app()->getRootPath() . 'public/admin/config.js';
            if (file_exists($filename)) {
                $domain = $this->request->domain(); // 获取当前域名
                $domainArr = explode("//", $domain);
                $wsUrl = 'ws://' . $domainArr[1] . ':2346';
                $wsType = 1;
                if ($domainArr[0] == 'https') {
                    $wsUrl = 'wss://' . $domainArr[1] . '/wss';
                     $wsType =2;
                }
            $content = <<<EOT
const APP_CONFIG = {
    APP_NAME: "{$appName}",
    API_URL: "{$domain}",
    WS_URL: "{$wsUrl}"
};
EOT;
                try {
                    // 创建并写入文件
                    file_put_contents($filename, $content);
                    $settings->domain= $domain;
                    $settings->ws_url= $wsUrl;
                    $settings->ws_type=  $wsType;
        			$settings->file_path= $domain.'/';
                    $settings->save();
                    $filePath = app()->getRootPath(). "public/assets/js/service_1.js";
                    $createPath = app()->getRootPath(). "public/assets/js/service.js";
                    if (!file_exists($createPath)) {
                        $js = @file_get_contents($filePath);//读取JS文件内容
                        $js=str_replace('doadmin_url',$domain,$js);//替换域名
                        file_put_contents($createPath, $js);//覆盖JS文件    
                    }
                     cookie('install', true);
                } catch (Exception $e) {
    
                }
    
            }
        }
        return View::fetch('index',);
               
    }
  
   
  
   
   
}
